import speech_recognition as sr

def recognize_speech():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("请说话...")
        recognizer.adjust_for_ambient_noise(source)  # 自动降噪
        audio = recognizer.listen(source)  # 录音

    try:
        text = recognizer.recognize_google(audio, language="zh-CN")  # 语音转文字
        print("识别结果:", text)
        return text
    except sr.UnknownValueError:
        print("无法识别语音")
    except sr.RequestError:
        print("请求失败，请检查网络")

    return None

# 运行
# user_input = recognize_speech()
# if user_input:
#     print("最终文本:", user_input)

